package com.david.mydemo.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
public class SmsUtil {

    /**
     * 发送短信
     *
     * @param acsClient
     * @param region
     * @param signName     签名
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param        参数
     */
    public static void sendMessage(IAcsClient acsClient, String region, String signName, String templateCode, String phoneNumbers, String param) {
        SendSmsRequest request = new SendSmsRequest();
        request.setRegionId(region);//设置地域编码
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":\"" + param + "\"}");
        try {
            SendSmsResponse response = acsClient.getAcsResponse(request);
            log.info("短信发送成功，返回：{}", JSONObject.toJSONString(response));
        } catch (ClientException e) {
            log.error("短信发送失败，异常信息：", e);
        }
    }

    /**
     * 随机生成验证码
     *
     * @param length 长度为4位或者6位
     * @return
     */
    public static Integer generateValidateCode(int length) {
        Integer code = null;
        if (length == 4) {
            code = new Random().nextInt(9999);//生成随机数，最大为9999
            if (code < 1000) {
                code = code + 1000;//保证随机数为4位数字
            }
        } else if (length == 6) {
            code = new Random().nextInt(999999);//生成随机数，最大为999999
            if (code < 100000) {
                code = code + 100000;//保证随机数为6位数字
            }
        } else {
            throw new RuntimeException("只能生成4位或6位数字验证码");
        }
        return code;
    }

    /**
     * 随机生成指定长度字符串验证码
     *
     * @param length 长度
     * @return
     */
    public static String generateValidateCode4String(int length) {
        Random rdm = new Random();
        String hash1 = Integer.toHexString(rdm.nextInt());
        String capstr = hash1.substring(0, length);
        return capstr;
    }

    public static void main(String[] args) throws Exception {
        String accessKey = "LTAI5tLU9A3xfGpsC2T8VgoS";
        String secretKey = "W2SUnl3zukiCOrCiWjelmzUiOBi6vR";
        String bucketName = "dispatch-pioneer";
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        String region = "cn-hangzhou";
        String product = "Green";
        String phone = "13109518617";
        String signName = "北京华信天图技术";
        String templateCode = "SMS_299785166";
        String code = generateValidateCode(6).toString();
        DefaultProfile profile = DefaultProfile.getProfile(region, accessKey, secretKey);
//        DefaultProfile.addEndpoint(region, product, endpoint); //地区要和图片存储的地区一致
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //调用阿里云提供的短信服务api完成发送短信
        sendMessage(acsClient, region, signName, templateCode, phone, code);
    }

}
